package android.got.tasks;

import com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager;

import android.accounts.Account;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;

public final class TaskSettings extends PreferenceActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.task_preferences);

        ListPreference accounts = (ListPreference) findPreference("accountName");
        String[] strAccounts = (String[]) getAvailableAccounts();
        accounts.setEntries(strAccounts);
        accounts.setEntryValues(strAccounts);

        TasksDBHelper dbHelper = new TasksDBHelper(getApplicationContext());

        ListPreference taskList = (ListPreference) findPreference("taskListID");
        String[] strTaskList = dbHelper.getTaskLists();
        taskList.setEntries(strTaskList);
        taskList.setEntryValues(strTaskList);
    }

    private CharSequence[] getAvailableAccounts() {
        GoogleAccountManager accountManager = new GoogleAccountManager(this.getApplicationContext());
        final Account[] accounts = accountManager.getAccounts();

        final int size = accounts.length;
        String[] names = new String[size];

        for (int i = 0; i < size; i++) {
            names[i] = accounts[i].name;
        }

        return names;
    }

}
